package cn.lzwei.bilimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.lzwei.bilimall.product.entity.AttrEntity;
import cn.lzwei.bilimall.product.service.AttrAttrgroupRelationService;
import cn.lzwei.bilimall.product.service.AttrService;
import cn.lzwei.bilimall.product.service.CategoryService;
import cn.lzwei.bilimall.product.vo.AttrGroupAttrVo;
import cn.lzwei.bilimall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.lzwei.bilimall.product.entity.AttrGroupEntity;
import cn.lzwei.bilimall.product.service.AttrGroupService;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author lizewei
 * @email Lizewei2105@163.com
 * @date 2022-12-21 15:39:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取分类下的属性分组以及每个分组下的基本属性：用于商品发布的规格参数设置
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttrsByCategory(@PathVariable(value = "catelogId") Long catelogId){
        List<AttrGroupAttrVo> attrGroupAttrVos=attrGroupService.getAttrGroupAndAttrsByCategory(catelogId);
        return R.ok().put("data",attrGroupAttrVos);
    }
    /**
     * 属性组批量关联属性
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrRelationVo> relationVo){
        attrAttrgroupRelationService.addBatch(relationVo);
        return R.ok();
    }
    /**
     * 查询分组已经关联的属性
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrGroupRelation(@PathVariable(name = "attrgroupId") Long attrgroupId){
        List<AttrEntity> entities=attrService.attrGroupRelation(attrgroupId);

        return R.ok().put("data", entities);
    }
    /**
     * 批量删除属性与分组的关联
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrGroupRelation(@RequestBody AttrRelationVo[] attrRelationVo){
        attrAttrgroupRelationService.deleteBatch(attrRelationVo);
        return R.ok();
    }
    /**
     * 查询分组可以新增关联的属性
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noAttrGroupRelation(@RequestParam Map<String, Object> params,@PathVariable(name = "attrgroupId") Long attrgroupId){
        PageUtils page = attrService.noAttrGroupRelation(params,attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 列表：三级分类+模糊查询
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(name = "catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //1.获取分类id
        Long catelogId = attrGroup.getCatelogId();
        //2.根据分类id获取分类路径
        attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
